//
//  NumberOptions.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/19/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

final class NumberOptionsCell: UITableViewCell {
    
    // MARK: - Outlets
    
    private var onlyChat = ToggleView ()
    private var noReseller = ToggleView ()
    private var protectNumber = ToggleView ()
    
    private lazy var togglesStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .fill
        stack.distribution = .fillEqually
        stack.spacing = 8
        return stack
    }()
    
    private lazy var infoView: UIView = {
        return self.createInfoView()
    }()
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func getData (completion: (Bool, Bool, Bool) -> Void) {
        completion(onlyChat.isOn(), noReseller.isOn(), protectNumber.isOn())
    }
    
}

extension NumberOptionsCell {
    
    private func addViews () {
        self.contentView.addSubview(togglesStack)
        self.contentView.addSubview(infoView)
        
        onlyChat.makeToggleView(name: "Общение только в чате", isOn: false)
        noReseller.makeToggleView(name: "Перекупщикам не звонить", isOn: false)
        protectNumber.makeToggleView(name: "Бесплатно защитить номер", isOn: false)
        
        togglesStack.addArrangedSubview(onlyChat)
        togglesStack.addArrangedSubview(noReseller)
        togglesStack.addArrangedSubview(protectNumber)
        
        self.addConstraints()
    }
    
    private func addConstraints () {
        let toggleItemsCount = togglesStack.arrangedSubviews.count
        
        togglesStack.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(64 * toggleItemsCount + 8 * toggleItemsCount - 1)
        }
        
        infoView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(togglesStack.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-8)
        }
    }
    
    /// Защита о спаме ( Small UI View )
    private func createInfoView () -> UIView {
        let view = UIView()
        view.backgroundColor = .rgb(209, 228, 219)
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
        
        let icon = UIImageView()
        icon.image = UIImage(named: "safe")
        icon.contentMode = .scaleAspectFill
        
        
        let titleLabel = UILabel()
        titleLabel.textAlignment = .left
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
        titleLabel.textColor = .rgb(72, 149, 110)
        titleLabel.sizeToFit()
        titleLabel.text = "Защита от спама"
        
        let paragraph = UILabel()
        paragraph.textAlignment = .left
        paragraph.textColor = .rgb(72, 149, 110)
        paragraph.font = UIFont.systemFont(ofSize: 13, weight: .regular)
        paragraph.numberOfLines = 0
        paragraph.sizeToFit()
        let str = "Мы не покажем ваш настоящий номер в объявлении, вместо него покупатели увидят временный. Звонки от них будут переадресоваться на ваш телефон, и мы защитим вас от SMS-спама во время и после продажи."
        
        let attrString = NSMutableAttributedString(string: str)
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 8
        attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: str.count))
        paragraph.attributedText = attrString
        
        
        view.addSubview(icon)
        view.addSubview(titleLabel)
        view.addSubview(paragraph)
        
        icon.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(18)
            make.width.equalTo(15)
            make.height.equalTo(18)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(icon.snp.trailing).offset(12)
            make.top.equalToSuperview().offset(18)
            make.trailing.equalToSuperview().offset(-16)
        }
        paragraph.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.trailing.equalToSuperview().offset(-16)
            make.leading.equalTo(titleLabel)
            make.bottom.equalToSuperview().offset(-12)
        }
        return view
    }
}
